﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iDMS.Models;
using System.Web.Helpers;

namespace iDMS.Controllers
{
    [CustomAuthorize(Roles = "Normal User")]
    public class ManufacturerController : Controller
    {
        DMSRepository db = new DMSRepository();
        //
        // GET: /Manufacturer/

        //public ActionResult Index()
        //{
        //    ManufacturerRepository mr = new ManufacturerRepository();
        //    var list = mr.FindAllManufacturers().OrderBy(m => m.Name);
        //    return View(list.ToList());
        //}

        private List<SelectListItem> populateCountries()
        {
            List<Country> country = db.CountriesGetAll().ToList();
            List<SelectListItem> countries = new List<SelectListItem>();
            foreach (Country item in country)
            {
                SelectListItem newItem = new SelectListItem();
                newItem.Text = item.Name;
                newItem.Value = item.CountryID.ToString();
                countries.Add(newItem);
            }
            return countries;
        }

        public ActionResult Index(string search)
        {
            var list = db.ManufacturersGetAll();
            if (!User.IsInRole("Administrator"))
            {
                list = list.Where(m => m.isHome == false);
            }

            if (!User.IsInRole("Archive"))
            {
                list = list.Where(m => m.isActive == true);
            }

            if (!string.IsNullOrEmpty(search))
            {
                list = list.Where(m => m.Name.ToUpper().Contains(search.ToUpper()));
            }

            list = list.OrderBy(m => m.Name);

            return View(list.ToList());
        }

        public ActionResult Newest(int amount)
        {
            var list = db.ManufacturersGetAll();
            if (!User.IsInRole("Administrator"))
            {
                list = list.Where(m => m.isHome == false);
            }

            if (!User.IsInRole("Archive"))
            {
                list = list.Where(m => m.isActive == true);
            }

            list = list.OrderByDescending(m => m.LastModified);

            if (amount > 1)
            {
                list = list.Take(amount);
            }



            return View("Index", list.ToList());
        }

        //
        // GET: /Manufacturer/Details/5

        public ActionResult Details(int id)
        {
            Manufacturer model = db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id);
            if (model == null)
            {
                return HttpNotFound();
            }
            return View(model);
        }

        public ActionResult DetailsWindow(int id)
        {
            Manufacturer model = db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id);
            if (model == null)
            {
                return Content("Manufacturer not found");
            }
            return PartialView("_DetailsWindow", model);
        }

        //
        // GET: /Manufacturer/Create

        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Create()
        {
            ViewBag.Countries = populateCountries();
            return View();
        }

        //
        // POST: /Manufacturer/Create

        [HttpPost]
        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                ViewBag.Countries = populateCountries();
                Manufacturer model = new Manufacturer();
                UpdateModel<Manufacturer>(model);
                model.isActive = true;
                model.isHome = false;
                model.LastModified = DateTime.Now;
                db.ManufacturerAdd(model);
                db.CommitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        //GET: /Manufacturer/Edit/5

        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Edit(int id)
        {
            ViewBag.Countries = populateCountries();
            Manufacturer model = db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id);
            return View(model);
        }


        //POST: /Manufacturer/Edit/5

        [HttpPost]
        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Edit(int id, FormCollection collection)
        {

            try
            {
                ViewBag.Countries = populateCountries();
                Manufacturer model = db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id);
                UpdateModel<Manufacturer>(model);
                model.LastModified = DateTime.Now;
                db.CommitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(id);
            }
        }

        //
        // GET: /Manufacturer/Delete/5

        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Delete(int id)
        {
            return View(db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id));
        }



        //
        // POST: /Manufacturer/Delete/5

        [HttpPost]
        [CustomAuthorize(Roles = "Managers")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Manufacturer manufacturer = db.ManufacturersGetAll().SingleOrDefault(m => m.ManufacturerID == id);
            if (manufacturer == null)
            {
                return HttpNotFound();
            }
            try
            {

                manufacturer.isActive = false;
                db.CommitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
